var consts;
function draw() {

    dataSource =  new YAHOO.util.LocalDataSource(data);
    var myAutoComp = new YAHOO.widget.AutoComplete("courseInput","autocomplete",dataSource);
    myAutoComp.queryMatchContains = true;
    myAutoComp.queryDelay = 0.01;
    myAutoComp.forceSelection = true;
    myAutoComp.maxResultsDisplayed = 20;
    myAutoComp.formatResult = function(oResultData, sQuery, sResultMatch) {
	return sResultMatch.split(sQuery.toUpperCase()).join("<b>"+sQuery.toUpperCase()+"</b>");
   
    };

    $('#hide').hide();
    $('#right').hide();
    $('#schedule').hide();
    $('#classContainer').hide('slow');
    $('#controls').hide();
    var canvas = document.getElementById("canvas");  
    if (canvas.getContext) { 
        var ctx = canvas.getContext("2d");  
		
	var my_gradient = ctx.createLinearGradient(canvas.height, 0,0,0 );
	my_gradient.addColorStop(0, "white");
	my_gradient.addColorStop(1, "rgb(200,200,200)");
 
	ctx.fillStyle = my_gradient;
 
	ctx.fillRect(0, 0, canvas.width, canvas.height);
	document.getElementById('courseInput').style.color = "gray"
	    }  
	//make sliders
    var days= new Array("m","t","w","th","f");
    for(var i=0;i<5;i++){	eval('$("#'+days[i]+'slider").slider({range: true,min: 800,max: 2000,step: 50,values: [ 800, 2000 ],slide: function( event, ui ) {$("#'+days[i]+'slider").children()[1].innerText = "start:" + (ui.values[0]%100==0)? (ui.values[0]-100)%1200+100 : (ui.values[0]-20-100)%1200+100;$("#'+days[i]+'slider").children()[2].innerText = (ui.values[1]%100==0)? (ui.values[1]-100)%1200+100 : (ui.values[1]-20-100)%1200+100 ;}});');
	$('#'+days[i]+'slider').children()[1].innerText="800";
	$('#'+days[i]+'slider').children()[2].innerText="800";
}
    
	
    
}
function addOrdering() {
	#$('#left').append('<select id="x"><option>Select a Class</option></select>before<select id="y"><option>Select a Class</option></select><br>');
	
}

function addclass() {
    var classname = document.getElementById('courseInput').value;
	    if(classname !="" &&classname !=document.getElementById('courseInput').defaultValue && $.inArray(classname, data)!=-1){
		var tag = document.createElement("class");
		tag.innerHTML = '<div id="tag" onclick="$(this).hide(200, function () { removeThis(this);  });"> '+classname+' </div>';
		document.getElementById('classes').appendChild(tag);
		document.getElementById('courseInput').value = "";
		populateSelects();
	}
 
}
function populateSelects(){
    var classes=document.getElementById('classes');
    var l = classes.childElementCount;
    document.getElementById("x").options.length = l-7;
    document.getElementById("y").options.length = l-7;
    if(l==7){
	document.getElementById("x").options[0]=new Option("no classes selected","");
	document.getElementById("y").options[0]=new Option("no classes selected","");}
    for(var i =7;i<l;i++){
	document.getElementById("x").options[i-7]=new Option(classes.children[i].innerText,classes.children[i].innerText);
	document.getElementById("y").options[i-7]=new Option(classes.children[i].innerText,classes.children[i].innerText);
    }
	
}

function removeThis(object){
    $(object).parent().remove();
    populateSelects();
}

function clearText(thefield){
    if (thefield.defaultValue==thefield.value){
	thefield.value = ""
	    thefield.style.color = "black";
    }
}
var numClasses=0;
function submit(){
    $('#classes').hide('slow');
    $('#left').hide('slow');
    $('#classContainer').show("slow");
    $('#right').show('slow');	
    $('#hide').show('slow');
    $('#schedule').show('slow');
    $('#controls').show('slow');
    classList = " ";
    classes = document.getElementById('classes')
		 classList+=classes.children[7].innerText;
	for(var i =8;i<classes.childElementCount;i++)
	    classList+="," +classes.children[i].innerText;

    document.getElementById("controls").innerHTML= "<h3>loading</h3>";
    consts=generateConstraints(" ");
    drawSchedule();
    //$.get("test.py", {classes:classList, constraints:consts}, function (response){ document.getElementById("controls").innerHTML=(response); var data=response.split(","); drawAllClasses(data);});
    xmlhttpPost("/cgi-bin/NTFTRunner/solve.py")
	
	
} 
function xmlhttpPost(strURL) {
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, true);
   self.xmlHttpReq.setRequestHeader('Content-Type', 'text');
    self.xmlHttpReq.onreadystatechange = function() {
      if (self.xmlHttpReq.readyState == 4) {
		
		response =self.xmlHttpReq.responseText;
		numClass=0;
		document.getElementById("classContainer").innerHTML = "";
		document.getElementById("controls").innerHTML = response;
		response=response.substring(response.indexOf("["));
		response=response.split("][");
		for(i in response){
			response[i]=response[i].replace(']','').replace('[','').split(") ,(")
			for(j in response[i]){
				response[i][j]=response[i][j].replace(')','').replace('(','').split(",")
			}
		}
		drawAllClasses(numClass);
      }
    }
    self.xmlHttpReq.send(getquerystring());
	
	

}

function getquerystring() {
	qstr = "class="+classList+"&consts="+consts
	return qstr
}
function generateConstraints(consts){
    
    var days= new Array("m","t","w","th","f");

    for(var i=0;i<5;i++){
	    consts+=",RTD "+days[i]+" 0 "+$("#"+days[i]+"slider").children()[1].innerText
			  +",RTD "+days[i]+" 0 "+$("#"+days[i]+"slider").children()[2].innerText;
    }
return consts;
}
function sho(){
    $('#classes').show('slow');
    $('#left').show('slow');
    $('#hide').hide('slow');
    $('#right').hide();
    $('#schedule').hide('slow');
    $('#classContainer').hide('slow');
    $('#controls').hide('slow');
} 


